package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.entity.UserInfo;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysCompanyContactConfigDTO;
import com.nineclock.system.mapper.SysCompanyContactConfigMapper;
import com.nineclock.system.pojo.SysCompanyContactConfig;
import com.nineclock.system.service.SysCompanyContactConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysCompanyContactConfigServiceImpl implements SysCompanyContactConfigService {
    @Autowired
    SysCompanyContactConfigMapper configMapper;

    @Override
    public void addCompanyContactConfig(SysCompanyContactConfigDTO configDTO) {
        //参数校验
        if (configDTO == null || StrUtil.isEmpty(configDTO.getName())) {
            throw new RuntimeException(ResponseEnum.INVALID_PARAM_ERROR.getMessage());
        }
        UserInfo userInfo = CurrentUserHolder.get();
        Long companyId = userInfo.getCompanyId();
        //添加数据
        SysCompanyContactConfig contactConfig = BeanHelper.copyProperties(configDTO, SysCompanyContactConfig.class);
        contactConfig.setStatus((short) 1); // 设置状态 为1， 表示可用
        contactConfig.setCompanyId(companyId);//目前先将该企业ID设置为1 , 后续完善
        contactConfig.setFieldType("string"); //设置字段类型为字符串类型

        configMapper.insert(contactConfig);
    }

    //查询并初始化
    @Override
    public List<SysCompanyContactConfigDTO> queryCompanyContactConfig() {
        List<SysCompanyContactConfigDTO> dtoList = new
                ArrayList<SysCompanyContactConfigDTO>();
        UserInfo userInfo = CurrentUserHolder.get();
        Long companyId = userInfo.getCompanyId();
        LambdaQueryWrapper<SysCompanyContactConfig> configWrapper = new
                LambdaQueryWrapper<>();
        configWrapper.eq(SysCompanyContactConfig::getCompanyId, companyId);
        List<SysCompanyContactConfig> sysCompanyContactConfigs = configMapper.selectList(configWrapper);
        if (CollectionUtil.isEmpty(sysCompanyContactConfigs)) {
            for (String fieldName : NcConstant.COMPANY_CONFIG_CONTACT_LIST) {
                SysCompanyContactConfig config = new SysCompanyContactConfig();
                config.setStatus((short) 1);
                config.setType("fixed");
                config.setName(fieldName);
                config.setFieldType("string");
                config.setCompanyId(companyId);
                configMapper.insert(config);

                dtoList.add(BeanHelper.copyProperties(config, SysCompanyContactConfigDTO.class));
            }
            return dtoList;
        }
        return BeanHelper.copyWithCollection(sysCompanyContactConfigs, SysCompanyContactConfigDTO.class);
    }

    //修改
    @Override
    public void updateCompanyContactConfig(Long id,Short status) {
        if (id!=null) {
            SysCompanyContactConfig sys = configMapper.selectById(id);
            if (sys==null) {
                throw new RuntimeException("不存在的id");
            }else {
                UserInfo userInfo = CurrentUserHolder.get();
                Long companyId = userInfo.getCompanyId();
                sys.setStatus(status);
                sys.setCompanyId(companyId);
                configMapper.updateById(sys);
                return;
            }
        }
        throw new RuntimeException("id不可为空");
    }

    //删除
    @Override
    public void deleteCompanyContactConfig(Long id) {
        configMapper.deleteById(id);
    }
}
